<?php
include_once("admin.inc.php");
$act = get_param('action');
$available = array('articles','addArticle','editArticle','delArticle','categories','addCategory','editCategory','delCategory','ueImgUpload');
if( !in_array($act, $available) ) die('非法操作');
$artModel = load_model('article');
$catModel = load_model('category');

switch($act):
case "articles":
	//$p = get_param("p");
	//$p = empty($p) ? 1 : intval($p);
	//$len = 20;
	//$total = $artModel->count_art();
	//$pages = 1;
	//if( $total>$len ){
		//$pages = ceil($total/$len);
	//}
	//$offset = ($p - 1) * $len;
	//$limit = "$offset, $len";
	$list = $artModel->get_art();
	$smarty->assign("list",$list);
	$smarty->display("admin/articles.html");
break;
case "addArticle":	
	if( !isset($_POST['submit']) ){
		$data = $catModel->get_cat();
		if( !empty($data) ){
			$smarty->assign("list",$data);
		}
		$smarty->display('admin/addArticle.html');		
	}else{
		$data = get_data();
		$data['author'] = 1;
		$data['addtime'] = time();
		$rs = $artModel->add_art($data);
		$status = $rs>0 ? 'Success' : 'Failed.'.$artModel->get_error();
		msg( $status , 'article.php?action=addArticle' );
	}
	
break;
case "editArticle":
	$id = intval(get_param('id'));
	if( empty($id) ){
		exit("非法操作");
	}
	if( get_param("title") ){
		$data = get_data();
	}else{
		$cats = $catModel->get_cat();
		if( !empty($cats) ){
			$smarty->assign("cats",$cats);
		}
		$list = $artModel->get_art("A.id=$id");
		$smarty->assign('list',$list[0]);
		$smarty->display('admin/editArticle.html');
	}
break;
case "delArticle":
	$id = intval(get_param('id'));
	$rs = 0;
	if( !empty($id) ){
		$rs = $artModel->del_art($id);
	}
	$status = $rs>0 ? 'Success' : 'Failed.'.$artModel->get_error();
	msg( $status , 'article.php?action=articles' );
break;
case "categories":
	$data = $catModel->get_cat();
	if( !empty($data) ){
		$smarty->assign("list",$data);
	}
	$smarty->assign("title","文章分类");
	$smarty->display('admin/categories.html');
break;
case "addCategory":
	$data = get_data();
	$rs = $catModel->add_cat($data);
	if( empty($rs) ){
		msg( $catModel->get_error() );
	}else{
		msg( 'Success',"article.php?action=categories" );
	}
break;
case "editCategory":
	$data = get_data();
	$rs = $catModel->edit_cat($data['cid'],$data);
	if( empty($rs) ){
		msg( $catModel->get_error() );
	}else{
		msg( 'Success',"article.php?action=categories" );
	}
break;
case "delCategory":
	$id = get_param('cid');	
	if( !empty($id) ){
		$rs = $catModel->del_cat($id);
		echo $rs;
	}else{
		echo 0;
	}
	exit;
break;
case "ueImgUpload":
	$up = load_class('Upload');
	$up->config(array('save_path'=>__UPLOAD__.'article/images/'));
	$rs = $up->save();
	$file = UPLOAD_URL.'article/images/'.$up->saveName;
	$title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
	$state = $rs ? "SUCCESS" : $up->get('error');
	echo "{'url':'".$file."','title':'".$title."','state':'".$state."'}";
break;
endswitch;
